<?php
namespace App\Http\Controllers;
use App\Form;
use App\Formitem;
use App\Post;
use App\Postmeta;
use App\ShopCat;
use App\ShopGoods;
use App\Shopmeta;
use App\ShopOrder;
use App\ShopUser;
use App\Tag;
use App\TagGroup;
use App\Usermeta;
use App\User;
use App\ShopCode;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Request;

class CompanyController extends Controller{
    private function CurrentUser(){
        $currentUser = Auth::user();
        $meta = Usermeta::where("user_id",$currentUser->id)->where("key","avatar")->first();
        $currentUser["avatar"] = $meta->value;
        $Post = Postmeta::select("post_id")->where("key","admin")->where("value",$currentUser->id)->get();
        if($num = count($Post)){
            $PostArr = [];
            for ($i=0;$i<$num;$i++){
                array_push($PostArr,$Post[$i]->post_id);
            }
            $currentUser["postArr"] = $PostArr;
        }
        $Shop = ShopUser::where("user_id",$currentUser->id)->first();
        if($Shop){
            $currentUser["shop"] = $Shop->id;
        }
        return $currentUser;
    }
    private function getQR($id,$path,$width=430){
        #$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxfca50078ea0ff5c1&secret=abbf5d3bc574fc27fe7802b6abfa1d0a';
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxfca50078ea0ff5c1&secret=2b9ea75c582a6b7b6be2a40aefea1327';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $json = curl_exec($ch);
        curl_close($ch);
        $json = json_decode($json);
        $url = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token='.$json->access_token;
        $post_data = array ("path" => $path,"width" => $width);
        $post_data = json_encode($post_data);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        if (curl_errno($ch)) {
            return false;
        }else{
            file_put_contents("upload/view/$id.jpg", $tmpInfo);
            return asset("upload/view/$id.jpg");
        }
    }
    private function ShopMetaSave($postId,$key,$value,$id,$del=null){
        if($id){
            $Postmeta = Shopmeta::find($id);
            if($del){
                $Postmeta->delete();
            }else{
                $Postmeta->value = $value;
                $Postmeta->save();
            }
        }else{
            $Postmeta = new Shopmeta();
            $Postmeta->shop_id = $postId;
            $Postmeta->key = $key;
            $Postmeta->value = $value;
            $Postmeta->save();
        }
    }
    private function metaSave($postId,$key,$value,$id,$del=null){
        if($id){
            $Postmeta = Postmeta::find($id);
            if($del){
                $Postmeta->delete();
            }else{
                $Postmeta->value = $value;
                $Postmeta->save();
            }
        }else{
            $Postmeta = new Postmeta();
            $Postmeta->post_id = $postId;
            $Postmeta->key = $key;
            $Postmeta->value = $value;
            $Postmeta->save();
        }
    }
    public function admin(Request $request){
        $Input = $request->input();
        $CurrentUser = $this->CurrentUser();
        if(isset($CurrentUser->postArr)){
            $Form = Form::whereIn("parent",$CurrentUser->postArr)->orderBy('id', 'desc');
            $name = null;
            if($request->has('id')){
                $Form = $Form->where('parent',$Input["id"])->where('item',$Input["item"])->get();
                if($Input["item"]!=0){
                    $name = Formitem::find($Input["item"]);
                    $name = json_decode($name->value);
                }else{
                    $name = [(object)Array("name"=>"联系人"),(object)Array("name"=>"联系电话"),(object)Array("name"=>"公司信息"),(object)Array("name"=>"公司简介")];
                }
            }else{
                $Form = $Form->paginate(20);
            }
            $TypeName = Array("service"=>"服务预约","active"=>"活动申请","join"=>"活动预约","user"=>"入驻申请");
            foreach ($Form as $f){
                $Post = null;
                if($f->parent!=0){
                    $Post = Post::find($f->parent);
                }
                $f->form = json_decode($f->form);
                $f->title = $f->parent!=0&&$Post?$Post->title:0;
                $f->typeName = $TypeName["$f->type"];
            }
            return view('company.admin',[
                "CurrentUser"=>$CurrentUser,
                "Forms"=>$Form,
                "Name"=>$name
            ]);
        }elseif(isset($CurrentUser->shop)){
            return redirect('shop');
        }else{
            return redirect('wx');
        }
    }
    public function FormEdit($id){
        $Form = Form::find($id);
        $value = json_decode($Form->form);
        $name = Array();
        if($Form->item){
            $items = Formitem::find($Form->item);
            $item = json_decode($items->value);
            for ($i=0;$i<count($item);$i++){
                array_push($name,$item[$i]->name);
            }
        }else{
            $name = ["联系人","联系电话","公司信息","公司简介"];
        }
        return view('company.formview',[
            "CurrentUser"=>$this->CurrentUser(),
            "Form"=>$Form,
            "Value"=>$value,
            "Name"=>$name
        ]);
    }
    public function shopEdit(){
        $CurrentUser = $this->CurrentUser();
        $Post = ShopUser::find($CurrentUser->shop);
        $Postmeta = Shopmeta::where('shop_id',$CurrentUser->shop)->get();
        $meta = [];
        foreach ($Postmeta as $m){
            $meta["$m->key"]= $m->value;
        }
        return view('company.shopEdit',[
            "Post"=>$Post,
            "meta"=>$meta,
            "CurrentUser"=>$this->CurrentUser()
        ]);
    }
    public function ShopSave(Request $request){
        $data = $request->input('Post');
        $Post = ShopUser::find($data["id"]);
        $Postmeta = Shopmeta::where('shop_id',$data["id"])->get();
        $meta = [];
        foreach ($Postmeta as $m){
            $meta["$m->key"]= $m->id;
        }


        $Post->name = $data["name"];
        $Post->content = $data["content"];
        $Post->cover = $data["cover"];
        $Post->summary = $data["summary"];
        $Post->address = $data["address"];
        $Post->phone = $data["phone"];

        if($Post->save()){
            if($data["viewQR"]){
                $this->ShopMetaSave($Post->id,'viewQR',$data["viewQR"],isset($meta)&&isset($meta['viewQR'])?$meta['viewQR']:null);
            }else{
                if(isset($data["createQR"])&&$data["createQR"]=='on'){
                    $QR = $this->getQR($Post->id,'pages/shop/shop?id='.$Post->id);
                    if($QR){
                        $this->ShopMetaSave($Post->id,'viewQR',$QR,isset($meta)&&isset($meta['viewQR'])?$meta['viewQR']:null);
                    }
                }else if(isset($meta)&&isset($meta['viewQR'])){
                    $this->ShopMetaSave($Post->id,'viewQR','',$meta['viewQR'],true);
                }
            }
            if(isset($data["coupon_use_limit"])){
                $this->ShopMetaSave($Post->id,'coupon_use_limit',$data["coupon_use_limit"],isset($meta)&&isset($meta['coupon_use_limit'])?$meta['coupon_use_limit']:null);
            }else if(isset($meta)&&isset($meta['coupon_use_limit'])){
                $this->ShopMetaSave($Post->id,'coupon_use_limit','',$meta['coupon_use_limit'],true);
            }

            $this->ShopMetaSave($Post->id,'startService',$data["startService"],isset($meta)&&isset($meta['startService'])?$meta['startService']:null);
            $this->ShopMetaSave($Post->id,'endService',$data["endService"],isset($meta)&&isset($meta['endService'])?$meta['endService']:null);
            $this->ShopMetaSave($Post->id,'delivery',$data["delivery"],isset($meta)&&isset($meta['delivery'])?$meta['delivery']:null);
            $this->ShopMetaSave($Post->id,'discount_full',$data["discount_full"],isset($meta)&&isset($meta['discount_full'])?$meta['discount_full']:null);
            $this->ShopMetaSave($Post->id,'discount_cut',$data["discount_cut"],isset($meta)&&isset($meta['discount_cut'])?$meta['discount_cut']:null);
            $this->ShopMetaSave($Post->id,'active',$data["active"],isset($meta)&&isset($meta['active'])?$meta['active']:null);

            return redirect('shop/edit')->with('success','修改成功');
        }else{
            return redirect()->back()->with('fail','修改失败');
        }
    }
    public function ShopCoupon(){
        $xcxs = Tag::where('type','xcx')->get();
        $services = Tag::where('type','couponCat')->get();
        $filter = Array('id'=>[],'name'=>[],'tag'=>[],'filterName'=>[]);
        foreach ($services as $s){
            $t_arr = Tag::where("parent_id",$s->id)->get();
            $_id = [];
            $_name = [];
            foreach ($t_arr as $_t){
                array_push($_id,$_t->id);
                array_push($_name,$_t->name);
            }
            array_push($filter['id'],$_id);
            array_push($filter['name'],$_name);
            array_push($filter['tag'],$s->id);
            array_push($filter['filterName'],$s->filter);
        }
        return view('company.coupon',[
            "xcxs"=>$xcxs,
            "services"=>$services,
            "filter"=>json_encode($filter),
            "CurrentUser"=>$this->CurrentUser()
        ]);
    }
    public function ShopCoupons(Request $request){
        $input = $request->input();
        $CurrentUser = $this->CurrentUser();
        $TagNum = Tag::where('type','xcx')->where('name','优惠券')->first();
        $Posts = Post::where('xcx',$TagNum->id)->where('user_id',$CurrentUser->id)->where('status','<',2);
        if($request->has("s")){
            $Posts = $Posts->where("title","like","%".$input["s"]."%");
        }
        if($request->has("cat")){
            $Posts = $Posts->where("tag",$input["cat"]);
        }
        $Posts = $Posts->orderBy('id', 'desc')->paginate(20);
        foreach ($Posts as $Post) {
            $user = User::find($Post->user_id);
            $Post->user = $user?$user->name:"0";
            $tag = Tag::find($Post->tag);
            $couponNum = Postmeta::where("post_id",$Post->id)->where("key","coupon_num")->first();
            $Post->tagName = $tag?$tag->name:"已删除";
            $Post->couponNum = $couponNum?$couponNum->value:"0";
        }
        $Cats = Tag::where("type","couponCat")->get();
        return view('company.coupons',[
            "Posts"=>$Posts,
            "Cats"=>$Cats,
            "CurrentUser"=>$CurrentUser
        ]);
    }
    public function ShopCouponSave(Request $request){
        $CurrentUser = $this->CurrentUser();
        $data = $request->input('Post');
        if($data["id"]){
            $Post = Post::find($data["id"]);
            $Postmeta = Postmeta::where('post_id',$data["id"])->get();
            $meta = [];
            foreach ($Postmeta as $m){
                $meta["$m->key"]= $m->id;
            }
            if(isset($data["created_at"]))$Post->created_at = $data["created_at"];
        }else{
            $Post = new Post();
        }

        $Post->title = $data["title"];
        $Post->content = $data["content"];
        $Post->cover = '';
        $Post->tag = $data["tag"];
        $Post->xcx = $data["xcx"];
        $Post->level = 0;
        $Post->author = $data["author"];
        $Post->user_id = $data["user_id"];
        $Post->status = $data["status"];

        if($Post->save()){
            $coupon_type = $data["coupon_type"];
            if(isset($data["activeKey_0"])){
                $key = [];
                $value = [];
                $arr = (object)[];
                $i = 0;
                while (isset($data['activeKey_'.$i])){
                    array_push($key,$data['activeKey_'.$i]);
                    array_push($value,$data['activeValue_'.$i]);
                    $i++;
                }
                $arr->key = $key;
                $arr->value = $value;
                $this->metaSave($Post->id,'service',json_encode($arr, JSON_UNESCAPED_UNICODE),isset($meta)&&isset($meta['service'])?$meta['service']:null);
            }else if(isset($meta)&&isset($meta['service'])){
                $this->metaSave($Post->id,'service','',$meta['service'],true);
            }
            if($data["viewQR"]){
                $this->metaSave($Post->id,'viewQR',$data["viewQR"],isset($meta)&&isset($meta['viewQR'])?$meta['viewQR']:null);
            }else{
                if(isset($data["createQR"])&&$data["createQR"]=='on'){
                    $QR = $this->getQR($Post->id,'pages/view/view?id='.$Post->id.'&&type=service');
                    if($QR){
                        $this->metaSave($Post->id,'viewQR',$QR,isset($meta)&&isset($meta['viewQR'])?$meta['viewQR']:null);
                    }
                }else if(isset($meta)&&isset($meta['viewQR'])){
                    $this->metaSave($Post->id,'viewQR','',$meta['viewQR'],true);
                }
            }
            if(isset($data["coupon_discount"])){
                $this->metaSave($Post->id,'coupon_discount',$data["coupon_discount"],isset($meta)&&isset($meta['coupon_discount'])?$meta['coupon_discount']:null);
            }else if(isset($meta)&&isset($meta['coupon_discount'])){
                $this->metaSave($Post->id,'coupon_discount','',$meta['coupon_discount'],true);
            }
            if(isset($data["coupon_cash"])){
                $this->metaSave($Post->id,'coupon_cash',$data["coupon_cash"],isset($meta)&&isset($meta['coupon_cash'])?$meta['coupon_cash']:null);
            }else if(isset($meta)&&isset($meta['coupon_cash'])){
                $this->metaSave($Post->id,'coupon_cash','',$meta['coupon_cash'],true);
            }
            if(isset($data["coupon_full"])&&isset($data["coupon_cut"])){
                $this->metaSave($Post->id,'coupon_full',$data["coupon_full"],isset($meta)&&isset($meta['coupon_full'])?$meta['coupon_full']:null);
                $this->metaSave($Post->id,'coupon_cut',$data["coupon_cut"],isset($meta)&&isset($meta['coupon_cut'])?$meta['coupon_cut']:null);
            }else if(isset($meta)&&isset($meta['coupon_full'])&&isset($data["coupon_cut"])){
                $this->metaSave($Post->id,'coupon_full','',$meta['coupon_full'],true);
                $this->metaSave($Post->id,'coupon_cut','',$meta['coupon_cut'],true);
            }
            $this->metaSave($Post->id,'startCoupon',$data["startCoupon"],isset($meta)&&isset($meta['startCoupon'])?$meta['startCoupon']:null);
            $this->metaSave($Post->id,'endCoupon',$data["endCoupon"],isset($meta)&&isset($meta['endCoupon'])?$meta['endCoupon']:null);
            $this->metaSave($Post->id,'coupon_type',$coupon_type,isset($meta)&&isset($meta['coupon_type'])?$meta['coupon_type']:null);
            $this->metaSave($Post->id,'coupon_id',$data["coupon_id"],isset($meta)&&isset($meta['coupon_id'])?$meta['coupon_id']:null);
            $this->metaSave($Post->id,'coupon_num',$data["coupon_num"],isset($meta)&&isset($meta['coupon_num'])?$meta['coupon_num']:null);
            $this->metaSave($Post->id,'coupon_limit',$data["coupon_limit"],isset($meta)&&isset($meta['coupon_limit'])?$meta['coupon_limit']:null);

            $tagGroup = TagGroup::where("post_id",$Post->id)->get();
            $tagOld = ["tag"=>[],"id"=>[]];
            foreach ($tagGroup as $tag){
                array_push($tagOld["tag"],$tag->tag);
                array_push($tagOld["id"],$tag->id);
            }
            if($data["arr"]){
                $tagNew = json_decode($data["arr"]);
                $tagAdd = [];
                $arrLength = count($tagNew);
                for ($i=0;$i<$arrLength;$i++){
                    $n = array_search($tagNew[$i],$tagOld["tag"]);
                    if($n===false){
                        array_push($tagAdd,$tagNew[$i]);
                    }else{
                        array_splice($tagOld["tag"],$n,1);
                        array_splice($tagOld["id"],$n,1);
                    };
                }

                $arrLength = count($tagAdd);
                for ($i=0;$i<$arrLength;$i++){
                    $newTag = new TagGroup();
                    $newTag->post_id = $Post->id;
                    $newTag->tag = $tagAdd[$i];
                    $newTag->save();
                }

                $arrLength = count($tagOld["tag"]);
                for ($i=0;$i<$arrLength;$i++){
                    $dalTag = TagGroup::find($tagOld["id"][$i]);
                    $dalTag->delete();
                }
            }else{
                $arrLength = count($tagOld["tag"]);
                for ($i=0;$i<$arrLength;$i++){
                    $dalTag = TagGroup::find($tagOld["id"][$i]);
                    $dalTag->delete();
                }
            }

            return redirect('coupon/edit/'.$Post->id)->with('success',isset($meta)?'修改成功':'新增成功');
        }else{
            return redirect()->back()->with('fail',isset($meta)?'修改失败':'新增失败');
        }
    }
    public function ShopCouponEdit($id){
        $xcxs = Tag::where('type','xcx')->get();
        $services = Tag::where('type','couponCat')->get();
        $Post = Post::find($id);
        $Postmeta = Postmeta::where('post_id',$id)->get();
        $meta = [];
        foreach ($Postmeta as $m){
            $meta["$m->key"]= $m->value;
        }
        $group = [];
        $filter = Array('id'=>[],'name'=>[],'tag'=>[],'filterName'=>[]);
        foreach ($services as $s){
            $t_arr = Tag::where("parent_id",$s->id)->get();
            $_id = [];
            $_name = [];
            foreach ($t_arr as $_t){
                array_push($_id,$_t->id);
                array_push($_name,$_t->name);
            }
            array_push($filter['id'],$_id);
            array_push($filter['name'],$_name);
            array_push($filter['tag'],$s->id);
            array_push($filter['filterName'],$s->filter);
        }
        $tagGroup = TagGroup::where('post_id',$id)->get();
        foreach ($tagGroup as $t){
            array_push($group,$t->tag);
        }
        return view('company.coupon',[
            "xcxs"=>$xcxs,
            "services"=>$services,
            "tagGroup"=>json_encode($group),
            "Post"=>$Post,
            "meta"=>$meta,
            "filter"=>json_encode($filter),
            "CurrentUser"=>$this->CurrentUser()
        ]);
    }
    public function ShopCouponDel($id){
        $Post = Post::find($id);
        $CurrentUser = $this->CurrentUser();
        if($Post->user_id===$CurrentUser->id){
            $Post->status = 9;
            if($Post->save()){
                return redirect()->back()->with('success','删除成功');
            }
        }
        return redirect()->back()->with('fail','删除失败');
    }
    public function ShopGoods(Request $request){
        $CurrentUser = $this->CurrentUser();
        $input = $request->input();
        $Goods = ShopGoods::where('status','<',2)->orderBy('id', 'desc')->where('parent_id',$CurrentUser->shop);
        if($request->has("s")){
            $Goods = $Goods->where("name","like","%".$input["s"]."%")->paginate(100);
        }else{
            $Goods = $Goods->paginate(20);
        }
        $Shops = ShopUser::find($CurrentUser->shop);
        foreach ($Goods as $g){
            $g->shop = $Shops?$Shops->name:'暂无';
            $catName = ShopCat::find($g->cat);
            $g->catName = $catName?$catName->name:"";
        }
        return view('company.goods',[
            "Goods"=>$Goods,
            "Shops"=>$Shops,
            "CurrentUser"=>$CurrentUser
        ]);
    }
    public function ShopGoodsAdd(){
        $CurrentUser = $this->CurrentUser();
        $Cats = ShopCat::where('parent_id',$CurrentUser->shop)->get();
        return view('company.goodsEdit',[
            "Cats"=>$Cats,
            "parent_id"=>$CurrentUser->shop,
            "CurrentUser"=>$CurrentUser
        ]);
    }
    public function ShopsGoodsSave(Request $request){
        $data = $request->input('Goods');
        if(isset($data['id'])){
            $Goods = ShopGoods::find($data['id']);
        }else{
            $Goods = new ShopGoods();
            $Goods->parent_id = $data["parent_id"];
        }

        $Goods->name = $data["name"];
        $Goods->price = $data["price"];
        $Goods->status = $data["status"];
        $Goods->more = $data["more"];
        $Goods->cat = $data["cat"];
        $Goods->kind = isset($data["kind"])?$data["kind"]:'';
        if(isset($data["created_at"])&&$data["created_at"])$Goods->created_at = $data["created_at"];
        if($Goods->save()){
            return redirect('shop/goods/edit/'.$Goods->id)->with('success',isset($data['id'])?'更新成功':'新增成功');
        }else{
            return redirect()->back()->with('fail',isset($data['id'])?'更新失败':'新增失败');
        }
    }
    public function ShopGoodsEdit($id){
        $Goods = ShopGoods::find($id);
        $Cats = ShopCat::where('parent_id',$Goods->parent_id)->get();
        return view('company.goodsEdit',[
            "Goods"=>$Goods,
            "Cats"=>$Cats,
            "parent_id"=>$Goods->parent_id,
            "CurrentUser"=>$this->CurrentUser()
        ]);
    }
    public function ShopDelEdit($id){
        $Goods = ShopGoods::find($id);
        $CurrentUser = $this->CurrentUser();
        if($Goods->parent_id===$CurrentUser->shop){
            $Goods->status = 9;
            if($Goods->save()){
                return redirect()->back()->with('success','删除成功');
            }
        }
        return redirect()->back()->with('fail','删除失败');
    }
    public function ShopsCat(Request $request){
        $CurrentUser = $this->CurrentUser();
        $Cats = ShopCat::orderBy('id', 'desc')->where('parent_id',$CurrentUser->shop)->paginate(20);
        $Shops = ShopUser::find($CurrentUser->shop);
        foreach ($Cats as $cat){
            $cat->shop = $Shops?$Shops->name:'暂无';
        }
        return view('company.shopCats',[
            "Cats"=>$Cats,
            "Shops"=>$Shops,
            "CurrentUser"=>$this->CurrentUser()
        ]);
    }
    public function ShopsCatSave(Request $request){
        $data = $request->input('Cat');
        if(isset($data['id'])){
            $Cat = ShopCat::find($data['id']);
            $Cat->name = $data["name"];
            $Cat->order_id = $data["order_id"];
            if($Cat->save()){
                return redirect()->back()->with('success','更新成功');
            }else{
                return redirect()->back()->with('fail','更新失败');
            }
        }else{
            $Cat = new ShopCat();

            $Cat->name = $data["name"];
            $Cat->parent_id = $data["parent_id"];
            if($data["order_id"]){
                $Cat->order_id = $data["order_id"];
            }else{
                $num = ShopCat::where('parent_id',$data["parent_id"])->count();
                $Cat->order_id = $num+1;
            }

            if($Cat->save()){
                return redirect()->back()->with('success','新增成功');
            }else{
                return redirect()->back()->with('fail','新增失败');
            }
        }
    }
    public function ShopsCatEdit($id){
        $Cat = ShopCat::find($id);
        return view('company.shopCatEdit',[
            "Cat"=>$Cat,
            "CurrentUser"=>$this->CurrentUser()
        ]);
    }
    public function ShopCatDel($id){
        $Shop = ShopCat::find($id);
        $CurrentUser = $this->CurrentUser();
        if($Shop->	parent_id===$CurrentUser->shop){
            if($Shop->delete()){
                return redirect()->back()->with('success','删除成功');
            }
        }
        return redirect()->back()->with('fail','删除失败');
    }
    public function shop(Request $request){
        $CurrentUser = $this->CurrentUser();
        $input = $request->input();
        $Orders = ShopOrder::orderBy('id', 'desc')->where('status',1)->where('post_id',$CurrentUser->shop);
        if($request->has("s")){
            $Orders = $Orders->where("trade","like","%".$input["s"]."%");
        }
        $Orders = $Orders->paginate(20);
        foreach ($Orders as $s) {
            $user = User::find($s->user_id);
            $s->user = $user?$user->name:"0";
        }
        return view('company.orders',[
            "Orders"=>$Orders,
            "CurrentUser"=>$CurrentUser
        ]);
    }
    public function OrderView($id){
        $Order = ShopOrder::find($id);
        $user = User::find($Order->user_id);
        $Order->user = $user?$user->name:"0";
        $Order->content = json_decode($Order->content);
        return view('company.ordersView',[
            "CurrentUser"=>$this->CurrentUser(),
            "order"=>$Order
        ]);
    }
    public function ShopCode(Request $request){
        $input = $request->input();
        $CurrentUser = $this->CurrentUser();
        $shop_id = $CurrentUser->shop?$CurrentUser->shop:0;
        $Code = ShopCode::where("shop_id",$shop_id)->orderBy('id', 'desc');
        if($request->has("status")){
            $Code = $Code->where('status',$input["status"]);
        }
        $Code = $Code->paginate(20);
        foreach ($Code as $c) {
            $shop = ShopUser::find($c->shop_id);
            $c->name = $shop?$shop->name:'无此商户';
            $order = ShopOrder::find($c->order_id);
            $c->order = $order?$order->trade:'';
        }
        return view('company.codes',[
            "CurrentUser"=> $CurrentUser,
            "codes"=>$Code
        ]);
    }
}
